﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Admin.Controllers
{
    public class UploadFileController : Controller
    {
        //
        // GET: /UploadFile/
        public ActionResult Index()
        {
            return View();
        }
        private bool isValidContentType(string contentType)
        {
            return contentType.Equals("image/png") || contentType.Equals("image/gif") ||
                contentType.Equals("image/jpg") || contentType.Equals("image/jpeg");
        }
        private bool isValidContentLength(int contentLength)
        {
            return ((contentLength / 1024) / 1024) < 1;
        }

        [HttpPost]
        public ActionResult Process(HttpPostedFileBase photo)
        
        {
            if (!isValidContentType(photo.ContentType))
            {
                ViewBag.Error = "Only jpg, png, gif & jpeg are allowed.";
                return View("Index");
            }
            else if (!isValidContentLength(photo.ContentLength))
            {
                ViewBag.Error = "loi";
                return View("Index");
            }
            else
            {
                if(photo.ContentLength>0)
                {
                    var fileName = Path.GetFileName(photo.FileName);
                    var path = Path.Combine(Server.MapPath("~/Areas/Admin/Content/images/sanpham"),fileName);
                    photo.SaveAs(path);
                    ViewBag.fileName = photo.FileName;

                }
            }
            ViewBag.fileName = photo.FileName;
            return View("insertProductDetails");
                  
        }
	}
}